package com.tian.controller;

import com.github.pagehelper.PageInfo;
import com.tian.pojo.Anime;
import com.tian.pojo.Beauty;
import com.tian.pojo.Scenery;
import com.tian.pojo.User;
import com.tian.service.AnimeService;
import com.tian.service.BeautyService;
import com.tian.service.SceneryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/view")
public class ViewController {
    @Autowired
    private SceneryService sceneryService;
    @Autowired
    private BeautyService beautyService;
    @Autowired
    private AnimeService animeService;
    @RequestMapping("/toScenery")
    public String toScenery(){
        return "scenery";
    }
    @RequestMapping("/getScenery")
    public String getScenery(Model model, @RequestParam(defaultValue = "1")Integer pageNum, @RequestParam(defaultValue = "12") Integer size){
        List<Scenery> list=sceneryService.getAllScenery(pageNum,size);
        PageInfo pageInfo=new PageInfo(list);
        model.addAttribute("pageInfo",pageInfo);
        return "scenery";
    }
    @RequestMapping("/getBeauty")
    public String getBeauty(Model model, @RequestParam(defaultValue = "1")Integer pageNum, @RequestParam(defaultValue = "12") Integer size){
        List<Beauty> list=beautyService.getAllBeauty(pageNum,size);
        PageInfo pageInfo=new PageInfo(list);
        model.addAttribute("pageInfo",pageInfo);
        return "beauty";
    }
    @RequestMapping("/getAnime")
    public String getAnime(Model model, @RequestParam(defaultValue = "1")Integer pageNum, @RequestParam(defaultValue = "12") Integer size){
        List<Anime> list=animeService.getAllAnime(pageNum,size);
        PageInfo pageInfo=new PageInfo(list);
        model.addAttribute("pageInfo",pageInfo);
        return "anime";
    }
    @RequestMapping("/toAddScenery")
    public String toAddScenery(Model model){
        return "/admin/addScenery";
    }
    @RequestMapping("/addScenery")
    public String addScenery(
                             @RequestPart("face") MultipartFile face){
        if(!face.isEmpty()){
            try {
                String id=UUID.randomUUID()+"l";
                Date date=new Date();
                //获取文件后缀名
                String originalFilename=face.getOriginalFilename();
                String finalFileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
                face.transferTo(new File("E://view//image//"+finalFileName));
                String path="/image/"+finalFileName;
                Scenery scenery=new Scenery(id,path,date);
                sceneryService.addScenery(scenery);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return "redirect:/view/getScenery";
    }
    @RequestMapping("/toAddBeauty")
    public String toAddBeauty(Model model){
        return "/admin/addBeauty";
    }
    @RequestMapping("/addBeauty")
    public String addBeauty(
            @RequestPart("face") MultipartFile face){
        if(!face.isEmpty()){
            try {
                String id=UUID.randomUUID()+"l";
                Date date=new Date();
                //获取文件后缀名
                String originalFilename=face.getOriginalFilename();
                String finalFileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
                face.transferTo(new File("E://view//image//"+finalFileName));
                String path="/image/"+finalFileName;
                Beauty beauty=new Beauty(id,path,date);
                beautyService.addBeauty(beauty);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return "redirect:/view/getBeauty";
    }
    @RequestMapping("/toAddAnime")
    public String toAddAnime(Model model){
        return "/admin/addAnime";
    }
    @RequestMapping("/addAnime")
    public String addAnime(
            @RequestPart("face") MultipartFile face){
        if(!face.isEmpty()){
            try {
                String id=UUID.randomUUID()+"l";
                Date date=new Date();
                //获取文件后缀名
                String originalFilename=face.getOriginalFilename();
                String finalFileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
                face.transferTo(new File("E://view//image//"+finalFileName));
                String path="/image/"+finalFileName;
                Anime anime=new Anime(id,path,date);
                animeService.addAnime(anime);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return "redirect:/view/getAnime";
    }
}
